﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class HighLowVoltageReportView : UserControl
    {
        private PRETWebServiceClient adapter = null;

        public HighLowVoltageReportView()
        {
            InitializeComponent();
            adapter = new PRETWebServiceClient();
        }

        private void voltageTimePanel1_showReportEvent(object sender, Controls.ShowVoleageReportViewEventArgs args)
        {
            try
            {
                this.voltageTimePanel.EnableControls(false);

                if (this.adapter == null)
                {
                    MessageBox.Show("HighLowVoltageReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.reportViewer.LocalReport.DataSources.Clear(); //clear report
                // 从后台获取特定时间段内的电表数据
                HighLowVoltageReport voltage = this.adapter.GetHighLowVoltageReport(args.StartTime, args.EndTime);
                if (voltage != null)
                {
                    Microsoft.Reporting.WinForms.ReportDataSource dataSet = new Microsoft.Reporting.WinForms.ReportDataSource("HighLowVoltageReportDS"); // set the datasource
                    this.reportViewer.LocalReport.DataSources.Add(dataSet);
                    List<HighLowVoltageReport> list = new List<HighLowVoltageReport> { voltage };
                    dataSet.Value = list;
                }
                this.reportViewer.LocalReport.Refresh();
                this.reportViewer.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("HighLowVoltageReportView.voltageTimePanel1_showReportEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.voltageTimePanel.EnableControls(true); }
        }
    }
}
